package uml_er_rea_tool.part;




import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;



import UML_ER.Element;
import UML_ER.OntologyReference;
import UML_ER.UML_ERFactory;
import UML_ER.diagram.edit.commands.ApplyOrUnapplyOntologyReferenceCommand;
import UML_ER.diagram.edit.commands.RefreshLabelsCommand;
import UML_ER.diagram.edit.parts.EntityEditPart;

public class AnnotateEntity implements IObjectActionDelegate {

	private Shell shell;
	private EntityEditPart selectedElement;
	
	/**
	 * Constructor for Action1.
	 */
	public AnnotateEntity() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		
		String menu = action.getId();
		OntologyReference newElement = UML_ERFactory.eINSTANCE.createOntologyReference();
		boolean apply = true;
		
		if(menu.equals("UML_ER_tool.EconomicResource")){
			newElement.setName("EconomicResource");
			newElement.setOntology("REA-EO");
		}
		if(menu.equals("UML_ER_tool.EconomicEvent")){
			newElement.setName("EconomicEvent");
			newElement.setOntology("REA-EO");
		}
		if(menu.equals("UML_ER_tool.Commitment")){
			newElement.setName("Commitment");
			newElement.setOntology("REA-EO");
		}
		if(menu.equals("UML_ER_tool.EconomicAgent")){
			newElement.setName("EconomicAgent");
			newElement.setOntology("REA-EO");
		}
		if(menu.equals("UML_ER_tool.Type")){
			newElement.setName("Type");
			newElement.setOntology("REA-EO");
		}
		if(menu.equals("UML_ER_tool.Group")){
			newElement.setName("Group");
			newElement.setOntology("REA-EO");
		}
		if(menu.equals("UML_ER_tool.UnApplyAnnotations")){
			apply = false;
		}
		ApplyOrUnapplyOntologyReferenceCommand.ApplyOrUnapplyStereotypeRequest annotationRequest = 
				new ApplyOrUnapplyOntologyReferenceCommand.ApplyOrUnapplyStereotypeRequest((Element)((View) selectedElement.getModel()).getElement(), newElement, apply);
	
		CompoundCommand command = new CompoundCommand();
		command.add(new RefreshLabelsCommand(selectedElement));
		command.add(new ICommandProxy( new ApplyOrUnapplyOntologyReferenceCommand(annotationRequest)));
		command.add(new RefreshLabelsCommand(selectedElement));
		selectedElement.getDiagramEditDomain().getDiagramCommandStack().execute(command);
		
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		selectedElement = null;
		System.out.println(action.getId());
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			if (structuredSelection.getFirstElement() instanceof EntityEditPart) {
				selectedElement = (EntityEditPart) structuredSelection
						.getFirstElement();
			}
			
		}
	}

}
